import axios, {Axios} from 'axios';
import { ElMessage } from 'element-plus';

const axiosInstance = axios.create({
	baseURL: 'http://localhost:8080',
	timeout: 10000
});


// 验证码
axiosInstance.fetchCaptcha = async () => {
	try {
		const response = await axiosInstance.get('/verify/code/createShearCaptcha', {responseType: 'blob'});
		if (response.headers['content-type'].includes('image')) {
			return URL.createObjectURL(response.data);
		}
	} catch (error) {
		console.error(error);
		ElMessage.error('获取验证码失败');
	}
};
// 登录
axiosInstance.accountLogin = async (studentID, password, verificationCode) => {
	try {
		const response = await axiosInstance.post(`/logins/login`, null, {
			params: {
				id: studentID,
				password: password,
				code: verificationCode
			}
		});
		return response.data;
	} catch (error) {
		console.error('登录请求失败:', error);
		throw error;
	}
};
// 重置密码
axiosInstance.resetPassword = async (newPassword) => {
	try {
		const response = await axiosInstance.post('/logins/changepassword', {newPassword});
		return response.data;
	} catch (error) {
		console.error('重置密码请求失败:', error);
		throw error;
	}
};
//输入id（学生）获取密保问题
axiosInstance.getStudentQuestion = async (studentID) => {
	try {
		const response = await axiosInstance.get(`/logins/StudentQuestion1`, {
			params: {sid: studentID}
		});
		return response.data;
	} catch (error) {
		console.error('An error occurred while fetching the problem:', error);
		throw error;
	}
};
//输入id（部门）获取密保问题
axiosInstance.getDepartmentQuestion = async (studentID) => {
	try {
		const response = await axiosInstance.get(`/logins/DepartmentQuestion1`, {
			params: {did: studentID}
		});
		return response.data;
	} catch (error) {
		console.error('An error occurred while fetching the problem:', error);
		throw error;
	}
};
// 验证密保问题
axiosInstance.validateAnswer = async (answer) => {
	try {
		const response = await axiosInstance.post('/logins/CheckPasswordprotection', null, {
			params: { protection1: answer }
		});
		return response.data;
	} catch (error) {
		console.error('验证密保问题请求失败:', error);
		throw error;
	}
};
// 重置密码
axiosInstance.submitResetPassword = async (newPassword) => {
	try {
		const response = await axiosInstance.put('/logins/changepassword', null, {
			params: {password: newPassword}
		});
		return response.data;
	} catch (error) {
		console.error('提交重置密码请求失败:', error);
		throw error;
	}
};


// 管理员——查询单个学生数据
axiosInstance.fetchStudent = async (studentID) => {
	try {
		const response = await axiosInstance.get('/students/findById',{
			params: { sid: studentID }}
		);
		return response.data;
	} catch (error) {
		console.error(error);
		ElMessage.error('获取学生信息失败');
	}
};
// 管理员——获取所有学生数据
axiosInstance.fetchStudents = async () => {
	try {
		const response = await axiosInstance.get('/students/getAll');
		return response.data;
	} catch (error) {
		console.error(error);
		throw error; // 抛出错误
	}
};
// 管理员——添加学生数据
axiosInstance.addStudent = async (student) => {
	try {
		const response = await axiosInstance.post('/students/addStudent', student);
		return response.data;
	} catch (error) {
		console.error(error);
		throw error; // 抛出错误
	}
};
// 管理员——删除学生数据
axiosInstance.deleteStudent = async (id) => {
	try {
		const response = await axiosInstance.delete(`/students/${id}`); // 使用 axios.delete 发送 DELETE 请求
		return response.data;
	} catch (error) {
		console.error(error);
		throw error; // 抛出错误
	}
};
// 管理员——更新学生数据
axiosInstance.updateStudent = async (student) => {
	try {
		const response = await axiosInstance.put(`/students/updateStudent`, student);
		return response.data;
	} catch (error) {
		console.error(error);
		throw error;
	}
};

// 管理员——查询单个部门详情
axiosInstance.fetchDepartment = async (departmentID) => {
	try {
	  const response = await axiosInstance.get('/departments/findById', {
		params: { did: departmentID }
	  });
	  return response.data;
	} catch (error) {
	  console.error(error);
	  ElMessage.error('获取部门信息失败');
	}
};

// 管理员——获取所有部门数据
axiosInstance.fetchDepartments = async () => {
	try {
		const response = await axiosInstance.get('/departments/findAll');
		return response.data;
	} catch (error) {
		console.error(error);
		throw error;
	}
};
// 管理员——添加部门数据
axiosInstance.addDepartment = async (department) => {
	try {
		const response = await axiosInstance.post('/departments/insertDepartment', department);
		return response.data;
	} catch (error) {
		console.error(error);
		throw error;
	}
};
// 管理员——删除部门数据
axiosInstance.deleteDepartment = async (id) => {
	try {
		const response = await axiosInstance.delete(`/departments/${id}`);
		return response.data;
	} catch (error) {
		console.error(error);
		throw error;
	}
};
// 管理员——更新部门数据
axiosInstance.updateDepartment = async (department) => {
	try {
		const response = await axiosInstance.put('/departments/updateDepartment', department);
		return response.data;
	} catch (error) {
		console.error(error);
		throw error;
	}
};


// 获取当前登录学生所有签到数据
axiosInstance.fetchActivityById = async (sid) => {
	try {
		console.log("id:"+sid);
		const response = await axiosInstance.get(`/findActivity/ById?sid=${sid}`);
		console.log("签到数据："+response.data);
		return response.data;
	} catch (error) {
		console.error(error);
		throw error;
	}
};
// 根据主类型查询学生数据
axiosInstance.fetchActivityByType = async (id, type) => {
	try {
		const response = await axiosInstance.get(`/findActivity/ByType?sid=${id}&type=${type}`);
		return response.data;
	} catch (error) {
		console.error(error);
		throw error;
	}
};
// 根据主类型+副类型查询学生数据
axiosInstance.fetchActivityByTypes = async (id, type, types) => {
	try {
		const response = await axiosInstance.get(`/findActivity/ByTypes?sid=${id}&type=${type}&types=${types}`);
		return response.data;
	} catch (error) {
		console.error(error);
		throw error;
	}
};


//签到表设计
axiosInstance.addActivity = async (Pbactivity) => {
	try {
		const response = await axiosInstance.post('/signInForm/add', Pbactivity);
		return response.data;
	} catch (error) {
		console.error(error);
		throw error;
	}
};
//签到表删除
axiosInstance.deleteActivity = async (id) => {
	try {
	  const response = await axiosInstance.delete(`/signInForm/delete?aid=${id}`);
	  return response.data;
	} catch (error) {
	  console.error(error);
	  throw error;
	}
};
//签到表更新
axiosInstance.updatePbActivity = async (pbActivityData) => {
  try {
    const response = await axiosInstance.put('/signInForm/update', pbActivityData);
    return response.data;
  } catch (error) {
    console.error(error);
    throw error;
  }
};
//签到表查询（单个）
axiosInstance.selectPbActivity = async (id) => {
	try {
	  const response = await axiosInstance.get(`/signInForm/select?aid=${id}`);
	  return response.data;
	} catch (error) {
	  console.error(error);
	  throw error;
	}
};
//签到表查询（部门所有）
axiosInstance.getDepartmentPbActivities = async (departmentId) => {
	try {
	  const response = await axiosInstance.get(`/signInForm/deselect?did=${departmentId}`);
	  return response.data;
	} catch (error) {
	  console.error(error);
	  throw error;
	}
};
//签到表查询（所有）
axiosInstance.fetchAllSignForms = async () => {
  try {
    const response = await axiosInstance.get('/signInForm/all');
    return response.data;
  } catch (error) {
    console.error('获取所有签到表数据时出错:', error);
    throw error; 
  }
};



//获取活动签到人数
axiosInstance.selectData = async (aid) => {
	try {
	  const response = await axiosInstance.get(`/activity?aid=${aid}`);
		return response.data; // 返回成功的数据
	} catch (error) {
	  console.error(error);
	  ElMessage.error('获取活动列表失败');
	}
};
// 发布证明，生成图片
axiosInstance.addPicture = async (aid) => {
	try {
	  const response = await axiosInstance.put(`/activity/addPicture?aid=${aid}`);
	  if (response.data.code == 0) {
		console.log("响应码是"+response.data.code);
		const imageUrl = response.data.data; // 返回的数据是图片的 URL
		ElMessage.success('后台已生成图片！');
		return imageUrl; // 返回图片 URL
	  } else {
		console.log("响应码是"+response.data.code);
		ElMessage.error(response.data.message);
	  }
	} catch (error) {
	  console.error(error);
	}
};

// 学生签到
axiosInstance.addSignInData = async (activityData) => {
	try {
	  const response = await axiosInstance.post('/signIn/dataAdd', activityData);
	  if (response.data.code == 0) {
		console.log("响应码是"+response.data.code);
		ElMessage.success('签到成功！');
		return response.data;
	  }else{
		console.log("响应码是"+response.data.code);
		ElMessage.error(response.data.message);
	  }

	  
	} catch (error) {
	  console.error(error);
	  throw error;
	}
};

// 推文设计
axiosInstance.addArticle = async (tweetData) => {
	try {
	  const response = await axiosInstance.post('/article/add', tweetData);
	  return response.data;
	} catch (error) {
	  console.error(error);
	  throw error;
	}
};
// 推文更新
axiosInstance.updateArticle = async (tweetData) => {
	try {
	  const response = await axiosInstance.put('/article/update', tweetData);
	  return response.data;
	} catch (error) {
	  console.error(error);
	  throw error;
	}
};
// 推文删除
axiosInstance.deleteArticle = async (tid) => {
	try {
	  const response = await axiosInstance.delete(`/article/delete?tid=${tid}`);
	  return response.data;
	} catch (error) {
	  console.error(error);
	  throw error;
	}
};
// 推文查询（单个）
axiosInstance.selectArticle = async (tid) => {
	try {
	  const response = await axiosInstance.get(`/article/select?tid=${tid}`);
	  return response.data;
	} catch (error) {
	  console.error(error);
	  throw error;
	}
};
// 推文查询（部门所有）
axiosInstance.selectArticlesByDid = async (departmentId) => {
	try {
	  const response = await axiosInstance.get(`/article/selectByDid?did=${departmentId}`);
	  return response.data;
	} catch (error) {
	  console.error(error);
	  throw error;
	}
};

//推文查询（所有）
axiosInstance.fetchAllArticles = async () => {
	try {
		const response = await axiosInstance.get('/article/all');
		return response.data;
	} catch (error) {
		console.error(error);
		ElMessage.error('获取文章列表失败');
	}
};




export default axiosInstance;
